//
//  LYQMineConsultingViewController.m
//  ZCProject
//
//  Created by Zheng on 2016/12/13.
//  Copyright © 2016年 Zheng. All rights reserved.
//

#import "LYQMineConsultingViewController.h"

#import "LYQConsultingHeaderCell.h"
#import "LYQConsultingFooterCell.h"
#import "LYQConsultingContentInfoCell.h"

#define kLYQConsultingHeaderCell_ID @"kLYQCOnsultingHeaderCell_ID"
#define kLYQConsultingFooterCell_ID @"kLYQConsultingFooterCell_ID"
#define kLYQConsultingContentInfoCell_ID @"kLYQConsultingContentInfoCell_ID"

@interface LYQConsultingHeaderView : UIView;

@property (nonatomic, strong) UILabel *remainingLbl; //剩余量

@end

@implementation LYQConsultingHeaderView

- (instancetype)init {
    self = [super init];
    if (self) {
        
        self.backgroundColor = RGB(225, 231, 235, 1);
        
        [self addSubview:self.remainingLbl];
        
        [_remainingLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.equalTo(self);
        }];
        
    }
    return self;
}

- (UILabel *)remainingLbl {
    if (_remainingLbl) {
        return _remainingLbl;
    }
    
    _remainingLbl = [[UILabel alloc] init];
    _remainingLbl.textColor = RGB(132, 145, 154, 1);
    _remainingLbl.font = kWFont(15);
    
    
    NSMutableAttributedString *textStr = [[NSMutableAttributedString alloc] initWithString:@"今日剩余回复量: 1/2"];
    NSRange range = NSMakeRange([textStr length]-3, 1);
    [textStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
    
    _remainingLbl.attributedText = textStr;
    
    return _remainingLbl;
}


@end

@interface LYQMineConsultingViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) LYQConsultingHeaderView *headerView; //今日回复余量
@property (nonatomic, strong) UIButton *detailBtn; //查看详情

@end

@implementation LYQMineConsultingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"咨询列表";
    
    self.listTableView.delegate = self;
    self.listTableView.dataSource = self;
    self.listTableView.estimatedRowHeight = 100;
    self.listTableView.rowHeight = UITableViewAutomaticDimension;
    self.listTableView.sectionFooterHeight = 0.0001f;
    [self.listTableView registerClass:[LYQConsultingHeaderCell class] forCellReuseIdentifier:kLYQConsultingHeaderCell_ID];
    [self.listTableView registerClass:[LYQConsultingFooterCell class] forCellReuseIdentifier:kLYQConsultingFooterCell_ID];
    [self.listTableView registerNib:[UINib nibWithNibName:@"LYQConsultingContentInfoCell" bundle:nil] forCellReuseIdentifier:kLYQConsultingContentInfoCell_ID];
    
    [self.view addSubview:self.detailBtn];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [_detailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-kWScale(35));
        make.left.equalTo(self.view).offset(kWScale(45));
        make.right.equalTo(self.view).offset(-kWScale(45));
        make.height.equalTo(@(kWScale(40)));
    }];
}

#pragma mark - lazy load subviews
- (UIButton *)detailBtn {
    if (_detailBtn) {
        return _detailBtn;
    }
    
    _detailBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _detailBtn.backgroundColor = kLYQBlue;
    _detailBtn.titleLabel.font = kWFont(16);
    [_detailBtn setTitle:@"查看资源详情" forState:UIControlStateNormal];
    [_detailBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _detailBtn.layer.cornerRadius = kWScale(20);
    [_detailBtn addTarget:self action:@selector(detailBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    return _detailBtn;
}

#pragma mark - 查看资源详情事件
- (void)detailBtnAction:(UIButton *)sender {
    NSLog(@"....查看资源详情....");
}


#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        LYQConsultingHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:kLYQConsultingHeaderCell_ID forIndexPath:indexPath];
        return cell;
    } else if (indexPath.row == 1) {
        LYQConsultingContentInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:kLYQConsultingContentInfoCell_ID forIndexPath:indexPath];
        
        if (indexPath.section == 0) {
            cell.contentLbl.text = @"xx月xx日,我们有一个xx人的团队去xxx.我们需要资源/服务.这次的团是农业方面的政府人员,最后你们拥有相关的资源,希望呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵呵";
        } else {
            cell.contentLbl.text = @"xx月xx日,我们有一个xx人的团队去xxx.";
        }
        
        return cell;
    } else {
        LYQConsultingFooterCell *cell = [tableView dequeueReusableCellWithIdentifier:kLYQConsultingFooterCell_ID forIndexPath:indexPath];
        return cell;
    }
}

#pragma mark - tableview delegate
#pragma mark 头视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return kWScale(49);
    } else {
        return kWScale(10);
    }
}

#pragma mark 自定义头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        if (_headerView) {
            return _headerView;
        }
        
        _headerView = [[LYQConsultingHeaderView alloc] init];
        
        return _headerView;
    } else {
        return nil;
    }
}

#pragma mark 分割线偏移量
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UIEdgeInsets insets = UIEdgeInsetsZero;
    
    if (indexPath.row == 0) {
        insets = UIEdgeInsetsMake(0, kWScale(65), 0, 0);
    } else {
        insets = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:insets];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:insets];
    }
}

#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
